package ibm.cognos.imt.template

import ibm.cognos.imt.chart.PieChart
import groovy.xml.MarkupBuilder

/**
 * Created with IntelliJ IDEA.
 * User: eric
 * Date: 5/13/14
 * Time: 3:50 PM
 * To change this template use File | Settings | File Templates.
 */
class PieChartTemplate extends AbstractTemplate{
    def pie
    PieChartTemplate(PieChart pieChart) {
        pie = pieChart
    }

    @Override
    def generateTemplateSpec(MarkupBuilder builder) {
        builder.v2_pieChart(maxHotspots: "1000", sliceStartAngle: "90", name: "${pie.name}", refQuery: "${pie.queryName}") {
            v2_pieTooltips()
            v2_legend {
                v2_legendPosition {
                    v2_legendPreset()

                }
                v2_legendTitle(refQuery: "${pie.queryName}") {
                    v2_chartTextContents {
                        v2_automaticText()
                    }
                    style {
                        defaultStyles {
                            defaultStyle(refStyle: "lx")
                        }
                    }
                }
                style {
                    defaultStyles {
                        defaultStyle(refStyle: "lg")
                    }
                }
            }
            v2_axisTitle(refQuery: "${pie.queryName}") {
                v2_chartTextContents {
                    v2_automaticText()
                }
                style {
                    defaultStyles {
                        defaultStyle(refStyle: "at")
                    }
                }
            }
            style {
                defaultStyles {
                    defaultStyle(refStyle: "ch")
                }
            }
            noDataHandler {
                contents {
                    block {
                        contents {
                            textItem {
                                dataSource {
                                    staticValue("No Data Available")
                                }
                                style {
                                    CSS(value: "padding:10px 18px;")
                                }
                            }
                        }
                    }
                }
            }
            v2_pieSlices {
                v2_solidPalette {
                    v2_solidPaletteEntries {
                        effects().each {
                            def defaultColor = it[0]
                            def gradientColor = it[1]
                            v2_solidPaletteEntry {
                                v2_fillEffect(defaultColor: "${defaultColor}") {
                                    v2_linearGradient {
                                        v2_gradientColor(gradientColor: "${defaultColor}")
                                        v2_gradientColor(colorPosition: "100", gradientColor: "${gradientColor}")
                                    }
                                }
                            }
                        }
                    }
                }
                chartNodes {
                    chartNode {
                        chartNodeMembers {
                            chartNodeMember(refDataItem: "${pie.slices}") {
                                chartContents {
                                    chartTextItem {
                                        dataSource {
                                            memberCaption()
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

            }
            if (pie.categories != null) {
                v2_category {
                    chartNodes {
                        chartNode {
                            chartNodeMembers {
                                chartNodeMember(refDataItem: "${pie.categories}")
                            }
                        }
                    }
                }
            }
            v2_defaultChartMeasure(refDataItem: "${pie.measure}")
            if (pie.categories != null) {

                v2_matrix {
                    v2_matrixColumns {
                        v2_matrixColumnLabels {
                            style {
                                defaultStyles {
                                    defaultStyle(refStyle: "al")
                                }
                            }
                        }
                        v2_axisTitle(refQuery: "${pie.queryName}") {
                            style {
                                defaultStyles {
                                    defaultStyle(refStyle: "at")
                                }
                            }
                            v2_chartTextContents {
                                v2_automaticText()
                            }
                        }
                    }
                }
            }
        }
    }

    @Override
    def getTemplateNode() {
        root = new XmlSlurper().parseText(getSpecStr())

        root
    }

    def effects() {
        [
                ["#8599D3", "#5876AE"],
                ["#E3AE6C", "#CD854E"],
                ["#839862", "#6C7F56"],
                ["#B7C873", "#AFB885"],
                ["#8484A8", "#525E7E"],
                ["#C0CCED", "#B0C2E5"],
                ["#8C5580", "#794067"],
                ["#C789BC", "#BB72BC"],
                ["#D5BAEF", "#C29FD1"],
                ["#83683F", "#604926"],
                ["#DCB05A", "#C09C52"],
                ["#F4DF9E", "#E4CF87"],
                ["#5F8A8C", "#537579"],
                ["#96C4B2", "#89B0A0"],
                ["#CBE8E7", "#BDD6D5"],
                ["#AE6564", "#875352"],
                ["#D88C6F", "#C47D61"],
                ["#E3C9B0", "#D2B2A5"],
                ["#848484", "#555555"],
                ["#a4a4a4", "#909090"],
                ["#C7C7C7", "#c1c1c1"]
        ]
    }
}
